//
//  HRBombBoxBottomOfView.m
//  Hear
//
//  Created by mac on 2017/8/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRBombBoxBottomOfView.h"
@interface HRBombBoxBottomOfView ()
@property (nonatomic, copy)SelectIndexBlock selectIndexBlock;
@property(nonatomic, strong)NSArray  *titleArr;
@property(nonatomic, assign)CGFloat buttonHeight;
@property(nonatomic, strong)UIButton *tempButton;

@end
@implementation HRBombBoxBottomOfView

+(void)showBombBoxBottomOfView:(NSArray *)titleArr selectindexBlock:(SelectIndexBlock)selectindexBlock addSubView:(id)subView{
    
    if (!titleArr.count) return;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRBombBoxBottomOfView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRBombBoxBottomOfView class]]){
        
        
        HRBombBoxBottomOfView *shareView = (HRBombBoxBottomOfView *)view;
        [shareView closeView];
        
    }else{
        [subView addSubview:showView];
        
        [showView loadView:titleArr selectindexBlock:selectindexBlock];
        
    }
}

- (void)loadView:(NSArray *)titleArr selectindexBlock:(SelectIndexBlock)selectindexBlock{
    
    self.titleArr = titleArr;
    
    _selectIndexBlock = selectindexBlock;
    
    
    self.buttonHeight = 50;

    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeView)];
    [self addGestureRecognizer:tap];
    
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, kFullHeight+titleArr.count*self.buttonHeight+15, kFullWidth, titleArr.count*self.buttonHeight+15)];
    showView.backgroundColor = CustomColor(240, 240, 240, 1);
    [self addSubview:showView];
    [UIView animateWithDuration:0.3 animations:^{
        showView.top = kFullHeight - titleArr.count*self.buttonHeight-15;
    }];
    
    for (int i = 0; i < titleArr.count; i ++) {
        UIButton *button = [UIButton buttonWithType:0];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle:titleArr[i] forState:0];
        [button setTitleColor:FirstTextColor forState:0];
        button.titleLabel.font = CustomUIFont(14);
        button.tag = 1+i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:button];
        
        if (i != titleArr.count-1){
            button.frame = CGRectMake(0, i*self.buttonHeight, showView.width, self.buttonHeight);
        }else{
            button.frame = CGRectMake(0, i*self.buttonHeight+15, showView.width, self.buttonHeight);
            [button setTitleColor:[UIColor redColor] forState:0];
        }
        if (i < titleArr.count-2){
            
            UILabel *line = [UILabel new];
            line.frame = CGRectMake(0, button.height-0.5, button.width, 0.5);
            line.backgroundColor =CustomColor(240, 240, 240, 1);;
            [button  addSubview:line];
        }
    }
}

- (void)buttonClick:(UIButton *)button{
    self.tempButton = button;
    [self closeView];
}

-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].top = kFullHeight+self.titleArr.count*self.buttonHeight+15;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.selectIndexBlock(self.tempButton.tag);
    }];
}

@end
